APP_NAME					:= titan-template
RELEASE_VERSION				:= 0.1.0
BUILD_TIME      			:= $(shell date +'%F %T %z')
LATEST_COMMIT_ID			:= $(shell git rev-parse HEAD)
CWD_PATH					:= $(shell pwd)
BIN_PATH					:= $(CWD_PATH)/bin
EXEC_PATH					:= $(BIN_PATH)/$(APP_NAME)
DEBUG_PATH					:= $(BIN_PATH)/$(APP_NAME)-debug
CMD_PATH 					:= $(CWD_PATH)/cmd
DOC_PATH					:= $(CWD_PATH)/doc
LINK_OPTIONS				:= -s -w -X 'gitlab.yunzhen-data.com/C-Chain/titan-template/configurations.BuildDate=$(BUILD_TIME)' -X 'gitlab.yunzhen-data.com/C-Chain/titan-template/configurations.GitCommitID=$(LATEST_COMMIT_ID)' -X 'gitlab.yunzhen-data.com/C-Chain/titan-template/configurations.ReleaseVersion=$(RELEASE_VERSION)'
GO_SOURCES					:= $(shell find $(CWD_PATH) -name '*.go')
GO_MOD						:= $(shell go list -m)

.PHONY:release
release: ent
	CGO_ENABLED=0 go build -tags=jsoniter -ldflags "$(LINK_OPTIONS)" -o ${EXEC_PATH} ${CMD_PATH}

.PHONY:debug
debug: ent
	CGO_ENABLED=0 go build -tags=jsoniter -gcflags="all=-N -l" -ldflags "$(LINK_OPTIONS)" -o ${DEBUG_PATH} ${CMD_PATH}

.PHONY:deps
deps:
	go mod download
	go install github.com/go-delve/delve/cmd/dlv@latest

.PHONY:tidy
tidy:
	go mod tidy

.PHONY:ent
ent:
	go generate ${CWD_PATH}/ent/

.PHONY:lint
lint: $(GO_SOURCES)
	for f in $(GO_SOURCES); do goimports-reviser -file-path $$f -project-name $(GO_MOD) -rm-unused; done
	golangci-lint run --fix

.PHONY:doc
doc:
	apidoc --config $(CWD_PATH)/apidoc.json --input $(CWD_PATH) --output $(DOC_PATH)

.PHONY:clean
clean:
	@rm -rf $(EXEC_PATH)
	@rm -rf $(DEBUG_PATH)
	@rm -rf $(DOC_PATH)
